﻿using System;
using System.Collections.Generic;
using System.Text;
using ZGRemote.Common.Message;
using ZGRemote.Common.Message.RemoteShell;
using ZGRemote.Common.Processor;

namespace ZGRemote.Server.Core.Handler
{
    [CanProcessMessage(typeof(DoCommandResponse))]
    public class RemoteShellHandler : HandlerBase<RemoteShellHandler>
    {
        public event Action<string> OutputText;

        public event Action<string> OutputError;

        public RemoteShellHandler() { }

        public override void Excute(MessageBase message)
        {
            if(message is DoCommandResponse m)
            {
                switch(m.ResponseType)
                {
                    case DoCommandResponseType.Message:
                        OutputText?.Invoke(m.Message);
                        break;
                    case DoCommandResponseType.Error:
                        OutputError?.Invoke(m.Message);
                        break;
                }
            }
        }

        public void CreateRemoteShell()
        {
            CreateShell message = new CreateShell();
            var res = SendMessage<CreateShellResponse>(UserContext, message);
            if (res.Success == false) 
            {
                throw new Exception($"{Properties.Resources.CreateRemoteShellError} {res.Message}");
            }
        }

        public void CloseRemoteShell()
        {
            CloseShell message = new CloseShell();
            SendMessage(UserContext, message);
        }

        public void DoCommand(string command)
        {
            DoCommand message = new DoCommand();
            message.Command = command;
            SendMessage(UserContext, message);
        }
    }
}
